#!/bin/bash

#generates *.h files with a model function and plots measurements and model function

#do for [i=0 method in "naiv daxpy"] {
#	fit f(x) "outfiles/" . method . ".w.plot.txt" using 1:2 via a,b,c,d
#	set print method . ".h"
#	print sprintf("#define %s_model(x) %e *x*x*x+ %e *x*x+ %e *x+ %e", method, a,b,c,d)
#	set print
#}

FILE=$1
echo $FILE
gnuplot <<EOF

set macro

#set xrange [96:2500]
#set yrange [0:7]

set terminal pdfcairo color enhanced
#font "Times-New-Roman,12" dashed

set style line 1 lt 2 lc rgb "red" lw 3
set style line 2 lt 2 lc rgb "orange" lw 2
set style line 3 lt 2 lc rgb "yellow" lw 3
set style line 4 lt 2 lc rgb "green" lw 2

f(x)=a*x**3+b*x**2+c*x+d
fnmeth(z) = "" . sprintf("%e",a) . "*x**3+" . sprintf("%e",b) . "*x**2+" . sprintf("%e",c) . "*x+" . sprintf("%e",d)
FITLIMIT=1e-6

set xlabel "Matrixgroesse n"
set ylabel "Rechenzeit [sec]"

#set style line 1 lt 1 lw 3 pt 3 linecolor rgb "red"
#set linestyle  1 lt 1 lw 6  # in gnuplot 3.7 script
#set style line 1 lc rgb "red" # in gnuplot 4.x script
#set style line 2 lc rgb "red" # in gnuplot 4.x script
#set style line 3 lc rgb "red" # in gnuplot 4.x script

method = "dgemm"
set title "Messung und Approx: " . method
fit f(x) "$FILE" using 1:2 via a,b,c,d
str=fnmeth(0)
dgemm(x)=@str
set print method . ".h"
print sprintf("#define %s_model(x) %e *x*x*x+ %e *x*x+ %e *x+ %e", method, a,b,c,d)
set print
#set term post
set output "./outfiles/" . method . ".pdf"
plot f(x), "$FILE" using 1:2 with lines title method

method = "method1"
set title "Messung und Approx: " . method
fit f(x) "$FILE" using 1:4 via a,b,c,d
str=fnmeth(0)
method1(x)=@str
set print method . ".h"
print sprintf("#define %s_model(x) %e *x*x*x+ %e *x*x+ %e *x+ %e", method, a,b,c,d)
set print
#set term post
set output "./outfiles/" . method . ".pdf"
plot f(x), "$FILE" using 1:3 with lines title method . "_1", "$FILE" using 1:4 with lines title method . "_2", "$FILE" using 1:5 with lines title method . "_3", "$FILE" using 1:6 with lines title method . "_4"

method = "method2"
set title "Messung und Approx: " . method
fit f(x) "$FILE" using 1:8 via a,b,c,d
str=fnmeth(0)
method2(x)=@str
set print method . ".h"
print sprintf("#define %s_model(x) %e *x*x*x+ %e *x*x+ %e *x+ %e", method, a,b,c,d)
set print
#set term post
set output "./outfiles/" . method . ".pdf"
plot f(x), "$FILE" using 1:7 with lines title method . "_1", "$FILE" using 1:8 with lines title method . "_2", "$FILE" using 1:9 with lines title method . "_3", "$FILE" using 1:10 with lines title method . "_4"

method = "method3"
set title "Messung und Approx: " . method
fit f(x) "$FILE" using 1:12 via a,b,c,d
str=fnmeth(0)
method3(x)=@str
set print method . ".h"
print sprintf("#define %s_model(x) %e *x*x*x+ %e *x*x+ %e *x+ %e", method, a,b,c,d)
set print
#set term post
set output "./outfiles/" . method . ".pdf"
plot f(x), "$FILE" using 1:11 with lines title method . "_1", "$FILE" using 1:12 with lines title method . "_2", "$FILE" using 1:13 with lines title method . "_3", "$FILE" using 1:14 with lines title method . "_4"

set output "./outfiles/all_models.pdf"
plot dgemm(x), method1(x), method2(x), method3(x)

set output "./outfiles/all_measures.pdf"
#plotstring = "plot " . system("ls -m -Q ./outfiles/*.w.plot.txt")
#@plotstring
plot "$FILE" using 1:2 with lines title "dgemm", "$FILE" using 1:6 with lines title "method1_4", "$FILE" using 1:10 with lines title "method2_4", "$FILE" using 1:14 with lines title "method3_4"
EOF
